package com.lanou.liuqixin.datebasedemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.liuqixin.datebasedemo.entity.SeriesEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 17/5/2.
 */

public class SpinnerSeriesAdapter extends BaseAdapter {
    private List<SeriesEntity> mList;
    private Context mContext;
    private boolean hasColor = false;
    private TextView mTextView;
    private SeriesEntity mSeriesEntity;


    public SpinnerSeriesAdapter(Context context) {
        mList = new ArrayList<>();
        mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            mSeriesEntity = new SeriesEntity();
            mSeriesEntity.setName("未知");
            return mSeriesEntity;
        }
        return mList.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        if (position == 0) {
            return 0;
        }
        return mList.get(position - 1).getIndex();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_spinner_item, parent, false);
            mTextView = (TextView) convertView;
            mSeriesEntity = (SeriesEntity) getItem(position);
            convertView.setTag(mTextView);
        } else {
            mTextView = (TextView) convertView.getTag();
        }
        if (hasColor) {
            mTextView.setTextColor(Color.WHITE);
        } else {
            mTextView.setTextColor(Color.BLACK);
        }
        mTextView.setText(mSeriesEntity.getName());
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }
        mTextView = (TextView) convertView;
        mSeriesEntity = (SeriesEntity) getItem(position);
        if (hasColor) {
       //     mTextView.setTextColor(Color.WHITE);
        } else {
            mTextView.setTextColor(Color.BLACK);
        }

        mTextView.setText(mSeriesEntity.getName());
        return convertView;
    }

    public SpinnerSeriesAdapter setColor(boolean white) {
        hasColor = white;
        notifyDataSetChanged();
        return this;
    }

    public void addAll(List<SeriesEntity> list) {
        this.mList.addAll(list);
        // 当数据发生变化时候通知适配器进行UI的刷新
        notifyDataSetChanged();
    }

    public void clear() {
        this.mList.clear();
        notifyDataSetChanged();
    }
}
